<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '10';
        $cquery = $this->db->get_all("select * from " . table('category'));
        if ($cquery) {
            foreach ($cquery as $k => $v) {
                $cats[$v['cid']] = $v['name'];
            }
        }
        $key = isset($_GET['key']) ? $_GET['key'] : '';
        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $wsql = '';
        if ($status) {
            $wsql = ' status = ' . $status . ' ';
        } elseif ($status == '0') {
            $wsql = ' status = 0 ';
        }
        if ($key) {
            $wsql = " title like '%" . $key . "%'";
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $gall = $this->db->get_all("select * from " . table('goods') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($gall) {
            foreach ($gall as $k => $v) {
                $sparr_id[] = $v['shop_id'];
            }
            $spin = implode(',', $sparr_id);
            $spall = $this->db->get_all("select id,name from " . table('shop') . " where id in (" . $spin . ")");
            foreach ($spall as $k => $v) {
                $sp_arr[$v['id']] = $v;
            }
            foreach ($gall as $k => $v) {
                if (isset($cats[$v['cid']])) {
                    $v['cats_cn'] = $cats[$v['cid']];
                }
                $v['shop_name'] = $sp_arr[$v['shop_id']]['name'];
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('goods') . $wsql);
            $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            $this->assign('data', $data);
        }
        $this->display();
    }
    function del()
    {
        $id = !empty($_POST['id']) ? $_POST['id'] : error_json('提交错误');
        if ($id) {
            $this->db->autocommit();
            $d1 = $this->db->delete(table('goods'), 'id in(' . $id . ')');
            $d2 = $this->db->delete(table('goods_img'), 'gid in (' . $id . ')');
            $d3 = $this->db->delete(table('goods_comment'), 'gid in (' . $id . ')');
            $d4 = $this->db->delete(table('goods_consult'), 'gid in (' . $id . ')');
            $d5 = $this->db->delete(table('goods_attr_index'), 'gid in (' . $id . ')');
            $d6 = $this->db->delete(table('goods_sku_img'), 'gid in (' . $id . ')');
            $d7 = $this->db->delete(table('goods_sku_index'), 'gid in (' . $id . ')');
            if ($d1 && $d2 && $d3 && $d4 && $d5 && $d6 && $d7) {
                $this->db->commit();
                right_json();
            } else {
                $this->db->rollback();
                error_json('删除失败');
            }
        }
    }
    function status()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('ID错误');
        $row = $this->db->get_one("select id,status from " . table('goods') . " where id = " . $id);
        if ($row['id'] && $row['status'] == '1') {
            $query = $this->db->update(table('goods'), array('status' => '0'), 'id = ' . $id);
        } elseif ($row['id'] && $row['status'] == '0') {
            $query = $this->db->update(table('goods'), array('status' => '1'), 'id = ' . $id);
        }
        if ($query) {
            showmsg('设置成功');
        } else {
            showmsg('设置失败');
        }
    }
    public function px_status()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : exit('error');
        $type = $_POST['type'];
        $this->db->query("update " . table('goods') . " set {$type} = ({$type} + 1)%2 where id = {$id} limit 1");
        $row = $this->db->get_one("select * from " . table('goods') . " where id = {$id}");
        echo $row[$type];
    }
    public function key_hot()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '12';
        $all = $this->db->get_all("select * from " . table('keyword') . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($all) {
            foreach ($all as $v) {
                $v['keyword'] = html_in($v['keyword']);
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('keyword'));
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function key_hot_edit()
    {
        $id = !empty($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('keyword') . " where id = " . $id);
            if ($row['id']) {
                $this->assign('row', $row);
            }
        }
        $this->display('goods/key_hot_edit');
    }
    function key_hot_save()
    {
        if ($_POST) {
            $setarr['keyword'] = P('keyword', '关键字不能为空');
            $setarr['px'] = P('px', '', '0');
            $id = P('id', 0);
            if ($id) {
                $query = $this->db->update(table('keyword'), $setarr, 'id = ' . $id);
            } else {
                $setarr['addtime'] = time();
                $query = $this->db->add(table('keyword'), $setarr);
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function key_del()
    {
        $id_in = isset($_POST['id']) ? $_POST['id'] : error_json('没有传递参数');
        $query = $this->db->delete(table('keyword'), 'id in(' . $id_in . ');');
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
    public function key_hot_status()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : exit('error');
        $type = $_POST['type'];
        $this->db->query("update " . table('keyword') . " set {$type} = ({$type} + 1)%2 where id = {$id} limit 1");
        $row = $this->db->get_one("select id,keyword,hot from " . table('keyword') . " where id = {$id}");
        echo $row[$type];
    }
    public function key_px_update()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : exit('id error');
        $px = isset($_POST['px']) ? $_POST['px'] : exit('px error');
        $query = $this->db->update(table('keyword'), array('px' => $px), 'id = ' . $id);
        if ($query) {
            exit('1');
        }
    }
}